package com.sjgl.esdn.utils;

import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Scanner;
import java.util.UUID;

/**
 * 文件工具
 *
 * @author 缪恒铭
 * @date 2022/08/17
 */
public class ImageUtilsESDN {
    /**
     * 图片转字节（jpg）
     *
     * @param img img
     * @return {@link byte[]}
     * @throws Exception 异常
     */
    public static byte[] imageToByte(File img) throws Exception {
        byte[] bytes = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            BufferedImage bi;
            bi = ImageIO.read(img);
            ImageIO.write(bi, "jpg", baos);
            bytes = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.close();
        }
        return bytes;
    }

    /**
     * 文件转base64
     *
     * @param path 路径
     * @return {@link String}
     */
    public static String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            File file = new File(path);
            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = new String(Base64.encodeBase64(bytes), "UTF-8");
//            System.out.println("将文件[" + path + "]转base64字符串:" );//+ base64
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }

    /**
     * @Description: 将base64编码字符串转换为图片
     * @param file base64编码字符串
     * @param path 图片路径-具体到文件
     * @return
     */
    public static boolean generateImage(String file, String path) {
        // 解密
        try {
            // 去掉base64前缀 data:image/jpeg;base64,
            file = file.substring(file.indexOf(",", 1) + 1, file.length());
            byte[] b = Base64.decodeBase64(file);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            // 保存图片
            File t=new File(path);
            if(!t.getParentFile().exists()){
                t.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(t);
            out.write(b);
            out.flush();
            out.close();
            // 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String file=scanner.nextLine();
        String path="C:\\Users\\Administrator\\Desktop\\1.jpg";
        generateImage(file,path);
    }
}